<template>
  <el-dialog
    :title="!dataForm.bargainId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="砍价开始时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="砍价开始时间"></el-input>
    </el-form-item>
    <el-form-item label="砍价结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="砍价结束时间"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodsPirce">
      <el-input v-model="dataForm.goodsPirce" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="底价" prop="floorPrice">
      <el-input v-model="dataForm.floorPrice" placeholder="底价"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则(刀数)" prop="knifeNum">
      <el-input v-model="dataForm.knifeNum" placeholder="砍价规则(刀数)"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则(每刀最多看多少钱)" prop="knifePirce">
      <el-input v-model="dataForm.knifePirce" placeholder="砍价规则(每刀最多看多少钱)"></el-input>
    </el-form-item>
    <el-form-item label="砍价活动描述" prop="bargainDesc">
      <el-input v-model="dataForm.bargainDesc" placeholder="砍价活动描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          bargainId: 0,
          goodsId: '',
          createTime: '',
          endTime: '',
          goodsPirce: '',
          floorPrice: '',
          knifeNum: '',
          knifePirce: '',
          bargainDesc: ''
        },
        dataRule: {
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '砍价开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '砍价结束时间不能为空', trigger: 'blur' }
          ],
          goodsPirce: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          floorPrice: [
            { required: true, message: '底价不能为空', trigger: 'blur' }
          ],
          knifeNum: [
            { required: true, message: '砍价规则(刀数)不能为空', trigger: 'blur' }
          ],
          knifePirce: [
            { required: true, message: '砍价规则(每刀最多看多少钱)不能为空', trigger: 'blur' }
          ],
          bargainDesc: [
            { required: true, message: '砍价活动描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.bargainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.bargainId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/info/${this.dataForm.bargainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsId = data.bargain.goodsId
                this.dataForm.createTime = data.bargain.createTime
                this.dataForm.endTime = data.bargain.endTime
                this.dataForm.goodsPirce = data.bargain.goodsPirce
                this.dataForm.floorPrice = data.bargain.floorPrice
                this.dataForm.knifeNum = data.bargain.knifeNum
                this.dataForm.knifePirce = data.bargain.knifePirce
                this.dataForm.bargainDesc = data.bargain.bargainDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/${!this.dataForm.bargainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bargainId': this.dataForm.bargainId || undefined,
                'goodsId': this.dataForm.goodsId,
                'createTime': this.dataForm.createTime,
                'endTime': this.dataForm.endTime,
                'goodsPirce': this.dataForm.goodsPirce,
                'floorPrice': this.dataForm.floorPrice,
                'knifeNum': this.dataForm.knifeNum,
                'knifePirce': this.dataForm.knifePirce,
                'bargainDesc': this.dataForm.bargainDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
